package com.mask.sca.system.controller;


import com.mask.sca.core.UserContextHolder;
import com.mask.sca.core.entity.CurrentUserInfo;
import com.mask.sca.core.response.ApiResponse;
import com.mask.sca.system.entity.User;
import com.mask.sca.system.service.UserQueryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mask.sca.system.vo.UserRoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserQueryController {

    @Autowired
    private UserQueryService userQueryService;

    @GetMapping("/byId")
    public ApiResponse<User> findById(@RequestParam Long id) {
        return ApiResponse.success(userQueryService.findById(id));
    }

    @GetMapping("/byUsernameAndTenantId")
    public ApiResponse<User> findByUsernameAndTenantId(@RequestParam String username, @RequestParam String tenantId) {
        return ApiResponse.success(userQueryService.findByUsernameAndTenantId(username, tenantId));
    }

    @GetMapping("/all")
//    public ApiResponse<List<User>> findAll() {
    public ApiResponse<List<User>> findAll(@RequestHeader(value = "currentUserInfo", required = false) String userInfoJson) {
        CurrentUserInfo currentUser = UserContextHolder.getCurrentUser();
        return ApiResponse.success(userQueryService.findAll());
    }

    //?username=&phone=&status=&pageNum=1&pageSize=10
    @GetMapping("/page")
    public ApiResponse<IPage<User>> findPage(@RequestParam int pageNum, @RequestParam int pageSize) {
        return ApiResponse.success(userQueryService.findPage(pageNum, pageSize));
    }

    @GetMapping("/userWithRoles")
    public ApiResponse<List<UserRoleVO>> getUserWithRoles() {
        return ApiResponse.success(userQueryService.getUserWithRoles());
    }
} 